package com.example.budget3.months.graphs;

import java.util.ArrayList;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;

public class DonutGraph extends View {

	private ArrayList<DonutSlice> slices = new ArrayList<DonutSlice>();
	private Paint paint = new Paint();
	private Path path = new Path();
	
	private int thickness = 50;
	private float total = -1;
	public DonutGraph(Context context) {
		super(context);
	}
	public DonutGraph(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.TRANSPARENT);
		paint.reset();
		paint.setAntiAlias(true);
		float midX, midY, radius, innerRadius;
		path.reset();
		
		float currentAngle = 270;
		float currentSweep = 0;
		float totalValue = 0;
		float padding = 2;
		
		midX = getWidth()/2;
		midY = getHeight()/2;
		if (midX < midY){
			radius = midX;
		} else {
			radius = midY;
		}
		radius -= padding;
		innerRadius = radius - thickness;
		for (DonutSlice slice : slices){
			totalValue += slice.getValue();
		}
		int percent =(int) ((totalValue/total) * 100);
		
		if(total != -1){
			totalValue = total > totalValue ? total : totalValue;
			
		}
		
		for (DonutSlice slice : slices){
			if(slice.getValue() > 0){
				Path p = new Path();
				paint.setColor(slice.getColor());
				currentSweep = (slice.getValue()/totalValue)*(360);
				p.arcTo(new RectF(midX-radius, midY-radius, midX+radius, midY+radius), currentAngle+padding, currentSweep - padding);
				p.arcTo(new RectF(midX-innerRadius, midY-innerRadius, midX+innerRadius, midY+innerRadius), (currentAngle+padding) + (currentSweep - padding), -(currentSweep-padding));
				p.close();
				
				slice.setPath(p);
				slice.setRegion(new Region((int)(midX-radius), (int)(midY-radius), (int)(midX+radius), (int)(midY+radius)));
				canvas.drawPath(p, paint);
				
				currentAngle = currentAngle+currentSweep;
			}
		}
		
		paint.setColor(Color.GRAY);
		String text = percent + "%";
		Rect r2 = new Rect();
		this.paint.setTextSize((getWidth() > getHeight() ? getHeight(): getWidth()) /6);
		this.paint.getTextBounds(text, 0, text.length(), r2 );
		
		float textHeight = r2.height(); 
		float textWidth= r2.width(); 
		canvas.drawText(text, midX - (textWidth/2), midY + (textHeight/2), this.paint);

		
		
	}
	
	public ArrayList<DonutSlice> getSlices() {
		return slices;
	}
	public void setSlices(ArrayList<DonutSlice> slices) {
		this.slices = slices;
		postInvalidate();
	}
	public DonutSlice getSlice(int index) {
		return slices.get(index);
	}
	public void addSlice(DonutSlice slice) {
		this.slices.add(slice);
		postInvalidate();
	}
	
	public void setTotal(float value) {
		this.total = value;
		postInvalidate();
	}
	public int getThickness() {
		return thickness;
	}
	public void setThickness(int thickness) {
		this.thickness = thickness;
		postInvalidate();
	}
	
	public void removeSlices(){
		for (int i = slices.size()-1; i >= 0; i--){
			slices.remove(i);
		}
		postInvalidate();
	}

}
